<%@page import="java.util.Set"%>
<%@page import="java.util.Map"%>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@page import="org.amj.wn.SchemaItem.UIOptions"%>
<%@page import="org.amj.wn.UIElement"%>
<%@page import="org.amj.wn.Property"%>
<%@page import="org.amj.wn.admin.analytics.AnalyticeService"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Analytics</title>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body style="width: 90%;">
	<jsp:include page="admincommon.jsp"></jsp:include>
	
	<div class="warning">
		Please note that Analytics data is updated every 3 hours. 
		You may not get the most recent data.
		Next update is in <b><%= 180 - (int)Math.ceil((System.currentTimeMillis() - AnalyticeService.getLastUpdated()) / (1000 * 60)) %></b> minutes.
	</div>
	<%
		String property = request.getParameter("property");
		String operator = request.getParameter("operator");
		String value = request.getParameter("value");
		String analysisOn = request.getParameter("analysisOn");
		boolean filter = "on".equals(request.getParameter("filter"));
		
		if(analysisOn == null)
			analysisOn = Property.province.name();
		if(property == null)
			property = Property.province.name();
	%>
	
	<form method="post" action="analytics.jsp">
		<span class="section">Analysis on: 
		<select name="analysisOn" onchange="this.form.submit()">
			<%
				for(Property pr : Property.sortedByCaption()) {
					if(pr.getUiElement() == UIElement.FIELD && pr.getUiOption() != UIOptions.hidden) {
						%><option value='<%=pr%>' <%=pr.name().equals(analysisOn) ? "selected" : "" %>><%=pr.getCaption() %></option><%
					}
				}
			%>
		</select>
		</span>
		<span class="section">Filter: 
		<select name="property" onchange="this.form.submit()" >
			<%
				for(Property pr : Property.sortedByCaption()) {
					if(pr.getUiElement() == UIElement.FIELD && pr.getUiOption() != UIOptions.hidden) {
						%><option value='<%=pr%>' <%=pr.name().equals(property) ? "selected" : "" %>><%=pr.getCaption() %></option><%
					}
				}
			%>
		</select>
		<select name="operator" onchange="this.form.submit()" >
			<%
				if(operator == null || "".equals(operator))
					operator = FilterOperator.EQUAL.name();
				FilterOperator op = FilterOperator.EQUAL;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>>Is</option>
				<%
				op = FilterOperator.LESS_THAN_OR_EQUAL;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>>Less than or equals</option>
				<%
				op = FilterOperator.GREATER_THAN_OR_EQUAL;
				%>
				<option value='<%=op.name()%>' <%=op.name().equals(operator) ? "selected" : "" %>>Greater than or equals</option>
		</select>
		<select name="value" onchange="this.form.submit()" >
			<%
				Set<Object> possibleValues = AnalyticeService.possibleValues(Property.valueOf(property));
				boolean valueFound = false;
				String firstValue = null;
				for(Object val : possibleValues) {
					if(firstValue == null)
						firstValue = val == null ? "" : val.toString();
					if(value == null)
						value = val == null ? "" : val.toString();
					val = val == null ? "" : val;
					valueFound = valueFound || val.equals(value);
						%>
						<option value='<%=val%>' <%= val.equals(value) ? "selected" : "" %>><%=val %></option>
						<%
				}
				if(!valueFound)
					value = firstValue;
			%>
		</select>
		| Filter On? <input type="checkbox" name="filter" onchange="this.form.submit()" <%=filter ? "checked='checked'" : "" %>/> 
		</span>
	</form>
	<%
		Property analysisOnProp = Property.valueOf(analysisOn);
		Map<Object, Integer> result = filter ? AnalyticeService.analyze(Property.valueOf(property), FilterOperator.valueOf(operator), value, analysisOnProp) : AnalyticeService.analyze(null, null, null, analysisOnProp);
	
	%>
	<hr />
	<span class="section">Analysis on: <%=analysisOnProp.getCaption() %> (Filter is <span style="color: green"><%=filter ? "Applied: " + Property.valueOf(property).getCaption() + " " + operator + " '" + value + "'": "OFF" %></span>)</span>
	<div class="datagrid">
	<table cellpadding="0" cellspacing="0">
	<tr>
		<th><%=analysisOnProp.getCaption() %></th>
		<th width="100%">Number of Waqfeen</th>
	</tr>

	<%
		if(result != null) {
			int count = 0;
		for(Map.Entry<Object, Integer> entry : result.entrySet()) {
			count++;
			if(count > 1000) {
				%>
				<tr>
					<th colspan="2">Terminating because of too many records.</th>
				</tr>
				<%

				break;
			}
			Object key = (entry.getKey() == null || entry.getKey().equals("")) ? "(empty)" : entry.getKey(); 
			%>
			<tr>
				<th><%=key %></th>
				<td width="100%"><a href="search.jsp?property=<%=analysisOnProp.name()%>&operator=EQUAL&value=<%=entry.getKey() == null ? "" : entry.getKey()%><%=filter ? "&property2=" + Property.valueOf(property).name() + "&operator2=" + operator + "&value2=" + value : "" %>"><%=entry.getValue() %></a></td>
			</tr>
			<%
		}
		}
	
	%>
	</table>
	</div>
</body>
</html>